package com.winning.sjzc.zygl.service.impl;

import com.winning.sjzc.common.utils.ResponseEntity;
import com.winning.sjzc.zygl.dao.ZxxDao;
import com.winning.sjzc.zygl.dao.ZyxxDao;
import com.winning.sjzc.zygl.entity.Zxx;
import com.winning.sjzc.zygl.entity.Zyxx;
import com.winning.sjzc.zygl.service.ZyglService;
import org.apache.commons.collections4.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class ZyglServiceImpl implements ZyglService {

    @Autowired
    private ZyxxDao zyxxDao;

    @Autowired
    private ZxxDao zxxDao;

    @Override
    public ResponseEntity queryZyTree() {
        return ResponseEntity.ok().put("list", zyxxDao.queryZyTree());
    }

    @Override
    @Transactional
    public ResponseEntity saveZyxx(Zyxx zyxx) {
        Map<String,Object> param =  new HashedMap<String,Object>();
        param.put("zydm",zyxx.getZydm());
        param.put("zylbsm",zyxx.getZylbsm());
        List<Zyxx> zyxxes = zyxxDao.queryZyxxByParam(param);
        if (zyxxes.size() > 0) {
            return ResponseEntity.error(201, "值域代码已经存在");
        }
        zyxxDao.insert(zyxx);
        ResponseEntity responseEntity = new ResponseEntity();
        responseEntity.put("zy", zyxx);
        return responseEntity;
    }

    @Override
    @Transactional
    public ResponseEntity delZyxxByZydm(String zydm) {
        zxxDao.deleteByZydm(zydm);
        int deleteRows = zyxxDao.deleteByPrimaryKey(zydm);
        if (deleteRows == 0) {
            return ResponseEntity.error(201, "无该值域信息");
        }
        return ResponseEntity.ok();
    }

    @Override
    public ResponseEntity getZyxxByZydm(String zydm) {
        Zyxx zyxx = zyxxDao.selectByPrimaryKey(zydm);
        ResponseEntity responseEntity = new ResponseEntity();
        return responseEntity.put("zy", zyxx);
    }

    @Override
    @Transactional
    public ResponseEntity updateZyxx(Zyxx zyxx) {
        int updateRows = zyxxDao.updateByPrimaryKeySelective(zyxx);
        if (updateRows == 0) {
            return ResponseEntity.error(201, "无该值域信息");
        }
        ResponseEntity responseEntity = new ResponseEntity();
        responseEntity.put("zy", zyxx);
        return responseEntity;
    }

    @Override
    public List<Zxx> queryZxxByParam(Map<String, Object> param) {
        return zxxDao.queryZxxByParam(param);
    }

    @Override
    @Transactional
    public ResponseEntity saveZxx(Zxx zxx) {
        Zyxx zyxx = zyxxDao.selectByPrimaryKey(zxx.getZydm());
        if (zyxx == null) {
            return ResponseEntity.error(201, "值域信息不存在");
        }
        Zxx zxxPresent = zxxDao.selectByPrimaryKey(zxx);
        if (zxxPresent != null) {
            return ResponseEntity.error(201, "值信息已存在");
        }
        zxxDao.insert(zxx);
        return ResponseEntity.ok();
    }

    @Override
    @Transactional
    public ResponseEntity delZxx(Zxx zxx) {
        int deleteRows = zxxDao.deleteByPrimaryKey(zxx);
        if (deleteRows == 0) {
            return ResponseEntity.error(201, "值信息不存在");
        }
        return ResponseEntity.ok();
    }

    @Override
    public ResponseEntity getZxxByZydmAndZdm(Zxx zxx) {
        ResponseEntity responseEntity = new ResponseEntity();
        return responseEntity.put("zyx", zxxDao.selectByPrimaryKey(zxx));
    }

    @Override
    @Transactional
    public ResponseEntity updateZxx(Zxx zxx) {
        int updateRow = zxxDao.updateByPrimaryKeySelective(zxx);
        if (updateRow == 0) {
            return ResponseEntity.error(201, "值信息不存在");
        }
        return ResponseEntity.ok();
    }
}
